<template>
  <div class="border py-2 pl-[20px]" v-if="route.query?.queryType === 'queryScore'">
    <div class="pt-[5px] pb-[10px]"> 查分理由：{{ currentInfo.cfly || '--' }} </div>
    <a-button type="primary mr-1" @click="geOtherInfo('up')"> 上一个 </a-button>
    <a-button type="primary mr-1" :disabled="currentInfo.sfcl == 'N'" @click="geOtherInfo('down')">
      下一个
    </a-button>
    <a-button type="primary mr-1" @click="handleSfyc(true)"> 无问题 </a-button>
    <a-button @click="handleSfyc(false)" danger> 有问题 </a-button>

    <div class="" v-if="currentInfo.sfyc == 'Y'">
      <label class="w-100px text-red-500">问题原因：</label>
      <div>{{ currentInfo.clyj || '无' }}</div>
    </div>

    <div v-show="ifShow" class="mt-1 py-1 pr-1 border-t-1 border-gray-400">
      <div class="flex">
        <label class="w-100px"><span class="text-red-500">*</span>问题原因：</label>
        <a-textarea
          v-model:value="textWtyy"
          placeholder="请输入问题原因"
          :auto-size="{ minRows: 2, maxRows: 5 }"
          class="mb-1"
        />
      </div>
      <a-button type="primary" @click="handleSubmit()">提交</a-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, unref } from 'vue'
import { useMessage } from '/@/hooks/web/useMessage'
import { useRoute, useRouter } from 'vue-router'
import {
  updateByGetWycSubmit,
  updateByPostYcSubmit,
  getNextRowAutoId,
  getPrevRowAutoId,
} from '/@/api/queryScore'

defineProps({
  currentInfo: {
    type: Object,
    default: () => ({}),
  },
})

const emit = defineEmits(['success'])

const ifShow = ref(false)
const textWtyy = ref()

const route = useRoute()
const router = useRouter()
const { createMessage, createConfirm } = useMessage()

async function geOtherInfo(type) {
  createConfirm({
    content: `是否获取${type == 'up' ? '上一个' : '下一个'}？`,
    iconType: 'warning',
    onOk: async function () {
      let record
      if (type == 'up') {
        record = await getPrevRowAutoId({ successAutoId: route.query.successAutoId })
      } else if (type == 'down') {
        record = await getNextRowAutoId({
          successAutoId: route.query.successAutoId,
        })
      }
      if (record.code == 200 && record.data) {
        router.replace({
          name: 'FractionDetaile',
          query: {
            queryType: 'queryScore',
            autoId: record.data.autoId,
            kmdm: record.data.kmdm,
            kmpyzt: 'FORMAL',
            xsXm: record.data.xm,
            kdkskmId: record.data.ksbh,
            successAutoId: record.data.successAutoId,
          },
        })
      } else {
        createMessage.info('无更多数据')
      }
    },
  })
}

function handleSfyc(flg) {
  ifShow.value = !flg
  if (flg) {
    handleSubmit('请确定是否标注为无问题？')
  }
}

function handleSubmit(text?: string) {
  if (unref(ifShow) && !unref(textWtyy)) {
    createMessage.warning('请填写问题原因!')
    return false
  }

  createConfirm({
    content: text || `请确定是否标注为有问题？`,
    iconType: 'warning',
    onOk: async function () {
      let res: any = {}
      if (unref(ifShow)) {
        res = await updateByPostYcSubmit({
          successAutoId: route.query.successAutoId, //主键id(必传)
          clyj: unref(textWtyy),
        })
      } else
        res = await updateByGetWycSubmit({
          successAutoId: route.query.successAutoId, //主键id(必传)
        })

      if (res.code == 200) {
        ifShow.value = false
        textWtyy.value = ''
        createMessage.success('提交成功')
        emit('success')
      } else {
        createMessage.error('提交失败: ' + res.message)
      }
    },
  })
}
</script>

<style scoped lang="less"></style>
